{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Consecutive Numbers Sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Hard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #enumeration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: consecutiveNumbersSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #连续整数求和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个正整数 <code>n</code>，返回 <em>连续正整数满足所有数字之和为 <code>n</code>&nbsp;的组数</em> 。&nbsp;</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示</strong><strong>例 1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入: </strong>n = 5\n",
    "<strong>输出: </strong>2\n",
    "<strong>解释: </strong>5 = 2 + 3，共有两组连续整数([5],[2,3])求和后为 5。</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入: </strong>n = 9\n",
    "<strong>输出: </strong>3\n",
    "<strong>解释: </strong>9 = 4 + 5 = 2 + 3 + 4</pre>\n",
    "\n",
    "<p><strong>示例 3:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入: </strong>n = 15\n",
    "<strong>输出: </strong>4\n",
    "<strong>解释: </strong>15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示:</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code>​​​​​​​</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [consecutive-numbers-sum](https://leetcode.cn/problems/consecutive-numbers-sum/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [consecutive-numbers-sum](https://leetcode.cn/problems/consecutive-numbers-sum/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['5', '9', '15']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        n *= 2\n",
    "        ans = 0\n",
    "\n",
    "        i = 1\n",
    "        while i * i < n:\n",
    "            if n % i == 0:\n",
    "                if (n/i - i + 1) % 2 == 0:\n",
    "                    ans += 1\n",
    "            i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        k=0\n",
    "        num=0\n",
    "        while(k*k+k<2*n):\n",
    "            p=2*n-k*k-k\n",
    "            q=2*k+2\n",
    "            if(p%q==0):\n",
    "                num+=1\n",
    "            k+=1\n",
    "        \n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, N: int) -> int:\n",
    "        # 1个数时，必然有一个数可构成N\n",
    "        # 2个数若要构成N，第2个数与第1个数差为1，N减掉这个1能整除2则能由商与商+1构成N\n",
    "        # 3个数若要构成N，第2个数与第1个数差为1，第3个数与第1个数的差为2，N减掉1再减掉2能整除3则能由商、商+1与商+2构成N\n",
    "        # 依次内推，当商即第1个数小于等于0时结束\n",
    "        res, i = 0, 1\n",
    "        while N > 0:\n",
    "            res += N % i == 0\n",
    "            N -= i\n",
    "            i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        \n",
    "        \n",
    "        \n",
    "        # my solution ... \n",
    "        \"\"\"\n",
    "        s = (a1+ak)*k//2 = (a1+a1+k-1)*k//2\n",
    "        2s//k = 2a1 + k - 1\n",
    "        \"\"\"\n",
    "        \n",
    "        cnt = 0\n",
    "        for k in range(1, int((2*n)**0.5)+1):\n",
    "            if (2*n) % k == 0:\n",
    "                d = (2*n) // k\n",
    "                if d > k and (d + 1 - k) >= 2 and (d + 1 - k) % 2 == 0:\n",
    "                    cnt += 1\n",
    "        return cnt\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ans = 1\n",
    "        for i in range(1, int((2 * n) ** 0.5)):\n",
    "            if ((n + i * (i + 1) / 2) % (i + 1)) == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        res = 0\n",
    "        i = 1\n",
    "        while n >0:\n",
    "            if n%i==0:\n",
    "                res+=1\n",
    "            n-=i\n",
    "            i+=1\n",
    "        return res\n",
    "#对于几个数加和等于 n (a a+1 a+2 ) (n -1-2)/3能整除就能找到这个a 结果就可以加一\n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        return sum(t % i == 0 and (t // i + 1 - i) % 2 == 0 for i in range(1, int(sqrt(t)) + 1)) if (t := 2 * n) else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        n *= 2\n",
    "        x = 1\n",
    "        cnt = 0\n",
    "        while x < sqrt(n):\n",
    "            if n % x == 0 and (n // x - x + 1) % 2 == 0:\n",
    "                cnt += 1\n",
    "            x += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        i = 1\n",
    "        ans = 0\n",
    "        while i * i < 2 * n:\n",
    "            if 2 * n % i == 0:\n",
    "                mid = 2 * n // i\n",
    "                if i % 2 == 0:\n",
    "                    if mid % 2 != 0 and mid > i:\n",
    "                        ans += 1\n",
    "                else:\n",
    "                    if mid % 2 == 0 and mid > i:\n",
    "                        ans += 1\n",
    "            i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        count = 0\n",
    "        upper_bound = int((2 * n) ** 0.5)\n",
    "        \n",
    "        for k in range(1, upper_bound + 1):\n",
    "            if (n - k*(k-1)//2) % k == 0:\n",
    "                count += 1\n",
    "        \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        cnt = 1\n",
    "        for k in range(2, n):\n",
    "            a = (2 * n // k + 1 - k) // 2\n",
    "            if a < 1:\n",
    "                break\n",
    "            elif n == (2 * a + k - 1) * k // 2:\n",
    "                cnt += 1\n",
    "        return cnt\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        n*=2\n",
    "        res=0\n",
    "        for i in range(1, int(sqrt(n))+1):\n",
    "            if n%i==0 and (i+n//i)%2==1:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        def isKConsecutive(n: int, k: int) -> bool:\n",
    "            if k % 2:\n",
    "                return n % k == 0\n",
    "            return n % k != 0 and 2 * n % k == 0\n",
    "        \n",
    "        ans = 0\n",
    "        k = 1\n",
    "        while k * (k + 1) <= n * 2:\n",
    "            if isKConsecutive(n, k):\n",
    "                ans += 1\n",
    "            k += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        cnt = 0\n",
    "        max_n = 0\n",
    "        target = n\n",
    "        if target % 2 == 1:\n",
    "            max_n = int(pow((2*target+0.25),0.5)-0.5)\n",
    "        else:\n",
    "            max_n = int(pow(2*target,0.5))\n",
    "        \n",
    "        for num in range(max_n,1,-1):\n",
    "            temp = []\n",
    "            if num % 2 == 1:\n",
    "                if target%num == 0:\n",
    "                    cnt += 1\n",
    "                else:\n",
    "                    continue\n",
    "            else:\n",
    "                if (target%num)/num == 0.5:\n",
    "                    cnt += 1\n",
    "                else:\n",
    "                    continue\n",
    "        return cnt+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ans = 1\n",
    "        c = 0\n",
    "        for i in range(1, int(sqrt(n*2))) :\n",
    "            c += i\n",
    "            ans += int((n - c) % (i+1) == 0)\n",
    "\n",
    "    \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1, int (sqrt(2 * n)) + 1):\n",
    "            if (i % 2 and n % i == 0 and n / i - i // 2 > 0) or (\n",
    "                    i % 2 == 0 and n % (i / 2) == 0 and n / (i / 2) % 2 and n / i + (1 - i) / 2 > 0):\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "\n",
    "        m = int(sqrt(2 * n))\n",
    "\n",
    "        res = 0\n",
    "        \n",
    "        for k in range(1, m + 1):\n",
    "            if (n - k * (k - 1) // 2) % k == 0:\n",
    "                res += 1\n",
    "\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        # count = 0\n",
    "        # for k in range(1, int(sqrt(n))+1):\n",
    "        #     if (2*n) % k == 0 and (2*n) / k > k - 1:\n",
    "        #         count += 1\n",
    "\n",
    "        # return count\n",
    "\n",
    "        return sum(t % i == 0 and (t // i + 1 - i) % 2 == 0 for i in range(1, int(sqrt(t)) + 1)) if (t := 2 * n) else 0\n",
    "\n",
    "\n",
    "        # if n%2 == 0:\n",
    "        #     k = n//2\n",
    "        # else:\n",
    "        #     k = (n+1)//2\n",
    "        \n",
    "        # count = 0\n",
    "\n",
    "        # for i in range(2,k+1):\n",
    "        #     sum = 0\n",
    "        #     for j in range(1, i+1):\n",
    "        #         sum += j\n",
    "        #     end = i\n",
    "        #     start = 1\n",
    "        #     if sum == n:\n",
    "        #         count += 1\n",
    "        #         continue\n",
    "\n",
    "        #     while end <= k:\n",
    "        #         if sum < n:\n",
    "        #             sum -=start\n",
    "        #             end += 1\n",
    "        #             sum += end\n",
    "        #             start += 1\n",
    "        #         elif sum == n:\n",
    "        #             count +=1\n",
    "        #             break\n",
    "        #         else:\n",
    "        #             break\n",
    "\n",
    "        # return count + 1\n",
    "                \n",
    "        \n",
    "\n",
    "    \n",
    "\n",
    " \n",
    "            \n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(1, int((2*n) ** 0.5) + 1):\n",
    "            j = (2 * n) / i\n",
    "            if (2 * n) % i == 0:\n",
    "                y = (j - i + 1) / 2\n",
    "                if y.is_integer() and y > 0:\n",
    "                    x = j - y\n",
    "                    if x.is_integer() and x > 0:\n",
    "                        res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        a,c,l=1,0,(int(math.sqrt(1+8*n))-1)//2\n",
    "        while a<=l:\n",
    "            s1,s2=False,False\n",
    "            if a%2==1:\n",
    "                if n%a==0 and n//a>a//2:c+=1\n",
    "            else:\n",
    "                nn=n-a//2\n",
    "                if nn%a==0 and nn//a>=a//2:c+=1\n",
    "            a+=1\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ss = ans = 0\n",
    "        i = 1\n",
    "        while True:\n",
    "            ss += i\n",
    "            if ss > n: break\n",
    "            if (n - ss) % i == 0:\n",
    "                ans += 1\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ans=1\n",
    "        for i in range(2,n):\n",
    "            if i*(i+1)/2>n:\n",
    "                break\n",
    "            num=n/i\n",
    "            if i%2==0:\n",
    "                if num%1!=0.5:\n",
    "                    continue\n",
    "                if i/2>int(num):\n",
    "                    break\n",
    "                ans+=1\n",
    "            else:\n",
    "                if num%1!=0:\n",
    "                    continue\n",
    "                if i//2+1>num:\n",
    "                    break\n",
    "                ans+=1\n",
    "        return ans\n",
    "                     \n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        if n == 1 or n == 2:\n",
    "            return 1\n",
    "        if n == 3:\n",
    "            return 2\n",
    "        k = int((int(math.sqrt(8*n+1)) - 1)/2)\n",
    "        # if sqr - int(sqr) == 0:\n",
    "        #     k = int(sqr) + 1\n",
    "        # else:\n",
    "        #     k = int(sqr) + 2\n",
    "        # print(k)\n",
    "        ans = 1\n",
    "        for i in range(2, k+1):\n",
    "            # print(\"i\", i)\n",
    "            if i % 2 == 0:\n",
    "                if n/i - int(n/i) == 0.5:\n",
    "                    ans += 1\n",
    "            else:\n",
    "                if n%i == 0:\n",
    "                    ans += 1\n",
    "            # print(\"ans\", ans)\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ## 有可能不满足无后效性的问题\n",
    "        ans= 0\n",
    "        k = 1\n",
    "        while k*k<=2*n:\n",
    "            ans += (n-(k-1)*k/2)%k==0\n",
    "            k+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # def consecutiveNumbersSum(self, n: int) -> int:\n",
    "    #     count = 0\n",
    "    #     for start in range(1, n + 1):\n",
    "    #         total = 0\n",
    "    #         while total < n:\n",
    "    #             total += start\n",
    "    #             start += 1\n",
    "    #         if total == n:\n",
    "    #             count += 1\n",
    "    #     return count\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        count = 1  # count itself\n",
    "        i = 2\n",
    "        while i * (i + 1) / 2 <= n:\n",
    "            if (n - (i * (i + 1)) / 2) % i == 0:\n",
    "                count += 1\n",
    "            i += 1\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, N: int) -> int:\n",
    "        if N<=2:\n",
    "            return 1\n",
    "        c=0\n",
    "        for x in range(1,N):\n",
    "           s=x*(x+1)//2\n",
    "           if s<N:\n",
    "               if (N-s)%x==0:\n",
    "                   c+=1\n",
    "           elif s==N:\n",
    "               c+=1\n",
    "               break\n",
    "           else:\n",
    "               break\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ans=1\n",
    "        k=2\n",
    "        y=1\n",
    "        while(n>= k+ y):\n",
    "            if (n-y) % k == 0:\n",
    "                ans+=1\n",
    "            y += k\n",
    "            k+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        count = 0\n",
    "        i, end = 1, n << 1\n",
    "        while i**2 < end:\n",
    "            res = n - (i + 1)*i/2\n",
    "            if res%i ==0:\n",
    "                count += 1\n",
    "            i += 1\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        res, i, N = 0, 1, n\n",
    "        while N > 0:\n",
    "            res += N % i == 0\n",
    "            N -= i\n",
    "            i += 1\n",
    "        return res\n",
    "\n",
    "# 作者：yybeta\n",
    "# 链接：https://leetcode.cn/problems/consecutive-numbers-sum/solution/pythonchao-hao-li-jie-de-onsuan-fa-by-yybeta/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        def isKConsecutive(n: int, k: int) -> bool:\n",
    "            if k % 2:\n",
    "                return n % k == 0\n",
    "            return n % k and 2 * n % k == 0\n",
    "\n",
    "        ans = 0\n",
    "        k = 1\n",
    "        while k * (k + 1) <= n * 2:\n",
    "            if isKConsecutive(n, k):\n",
    "                ans += 1\n",
    "            k += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        temp = 1\n",
    "        count = 0\n",
    "        while temp * temp <= 2 * n:\n",
    "            if temp % 2 == 1:\n",
    "                if n % temp == 0:\n",
    "                    count += 1\n",
    "            else:\n",
    "                if n % temp == (temp / 2):\n",
    "                    count += 1\n",
    "            temp += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "               \n",
    "                        \n",
    "               return sum(n%p==p//2*(1-p%2) for p in range(1,int((2*n)**.5)+1))\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        return sum(t % i == 0 and (t // i + 1 - i) % 2 == 0 for i in range(1, int(sqrt(t)) + 1)) if (t := 2 * n) else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        n += n\n",
    "        ans = 0\n",
    "        for x in range(1, isqrt(n) + 1):\n",
    "            if n % x == 0 and (n // x - x) % 2:\n",
    "                ans += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        res = 0\n",
    "        # 从根号2n到1\n",
    "        for len in range(int(math.sqrt(2*n)),0,-1):\n",
    "            if 2*n % len == 0 and ((int(2*n/len)-len+1)&1) ==0:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ans = 1\n",
    "        for i in range(2, n):\n",
    "            if i * (i+1) > n * 2:\n",
    "                break\n",
    "            if (n % i == 0 and i % 2 == 1) or (n % i == i //2 and i % 2 == 0):\n",
    "                if n // i - (i - 1) // 2 > 0:\n",
    "                    ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        n += n\n",
    "        ans = 0\n",
    "        for x in range(1, isqrt(n) + 1):\n",
    "            if not n % x and (n // x - x) % 2:\n",
    "                ans += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ans,m=0,2*n\n",
    "        k=1\n",
    "        while k*(k+1)<=m:\n",
    "            if m%k==0 and (m//k-k)&1:\n",
    "                ans+=1\n",
    "            k+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        results = 1\n",
    "        for i in range(2, n):\n",
    "            start = (2 * n - i * (i - 1)) // (2 * i)\n",
    "            if start < 1:\n",
    "                break\n",
    "            reminder = (2 * n - i * (i - 1)) % (2 * i)\n",
    "            if reminder:\n",
    "                continue\n",
    "\n",
    "            results += 1\n",
    "\n",
    "        return results\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        def isKConsecutive(n: int, k: int) -> bool:\n",
    "            if k % 2:\n",
    "                return n % k == 0\n",
    "            return n % k and 2 * n % k == 0\n",
    "\n",
    "        ans = 0\n",
    "        k = 1\n",
    "        while k * (k + 1) <= n * 2:\n",
    "            if isKConsecutive(n, k):\n",
    "                ans += 1\n",
    "            k += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        res = i = 0\n",
    "        n *= 2\n",
    "        for i in range(1, n):\n",
    "            if i * i >= n: break\n",
    "            if n % i != 0:\n",
    "                continue\n",
    "            if ((n // i - (i - 1)) % 2 == 0):\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        div = 1\n",
    "        c1 = False\n",
    "        c2 = False\n",
    "        while div * (div + 1) <= 2 * n:\n",
    "            shang = n // div\n",
    "            yushu = n % div\n",
    "            if div % 2 and yushu == 0:\n",
    "                if shang >= (div - 1) // 2:\n",
    "                    ans += 1\n",
    "\n",
    "            elif div % 2 == 0 and yushu == div // 2:\n",
    "                if shang >= div // 2:\n",
    "                    ans += 1\n",
    "\n",
    "            div += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        # left=right=1\n",
    "        # s = 1\n",
    "        # res = 0\n",
    "        # while left<=n:\n",
    "        #     if s <n:\n",
    "        #         right+=1\n",
    "        #         s+=right\n",
    "        #     elif s>n:\n",
    "        #         s-=left\n",
    "        #         left+=1\n",
    "        #     else:\n",
    "        #         res += 1\n",
    "        #         s-=left\n",
    "        #         left+=1\n",
    "            \n",
    "        # return res\n",
    "\n",
    "        res = 1\n",
    "        for f in range(2,int((2*n)**0.5)+1):\n",
    "            if (2*n)//f<=f:\n",
    "                break\n",
    "            if f%2==1 and n%f==0:\n",
    "                res+=1\n",
    "            elif n%f>0 and (2*n)%f==0: \n",
    "                res+=1\n",
    "        return res\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        # res = 0\n",
    "        # if n>1:\n",
    "        #     for i in range(1,n//2+2):\n",
    "        #         if n%i == 0:\n",
    "        #             if i%2 == 1 and n/i>i//2:\n",
    "        #                 print(i)\n",
    "        #                 res += 1\n",
    "        #         elif (2*n)%i == 0 and i%2 == 0:\n",
    "        #             if (n)/i > i/2:\n",
    "        #                 print(i)\n",
    "        #                 res += 1\n",
    "        #     return res\n",
    "        # else:\n",
    "        #     return 1\n",
    "        res = 1\n",
    "        for k in range(2,n):\n",
    "            a = ((2*n)//k+1-k)//2\n",
    "            if a < 1:\n",
    "                break\n",
    "            elif n==((2*a)+k-1)*k//2:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        result = 0\n",
    "        for k in range(1,int(math.sqrt(2*n))+1):\n",
    "            if 2*n%k==0 and (2*n/k - k +1)%2 ==0:\n",
    "                result+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        def isKConsecutive(n: int, k: int) -> bool:\n",
    "            if k % 2:\n",
    "                return n % k == 0\n",
    "            return n % k and 2 * n % k == 0\n",
    "\n",
    "        ans = 0\n",
    "        k = 1\n",
    "        while k * (k + 1) <= n * 2:\n",
    "            if isKConsecutive(n, k):\n",
    "                ans += 1\n",
    "            k += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        k=1\n",
    "        num=0\n",
    "        while k*(k+1)<=2*n:\n",
    "            if k%2==0 and n/k!=int(n/k) and 2*n/k == int(2*n/k):\n",
    "                num+=1\n",
    "            if k%2==1 and n/k==int(n/k):\n",
    "                num+=1\n",
    "            k+=1\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        if n == 1: return 1\n",
    "        tmp, cnts, val = 2 * n, collections.defaultdict(int), 2\n",
    "        while val * val <= tmp:\n",
    "            if tmp % val == 0:\n",
    "                cnts[val] += 1\n",
    "                tmp //= val\n",
    "            else: val += 1\n",
    "        if tmp > 1: cnts[tmp] += 1\n",
    "        vals = [[key, cnts[key]] for key in cnts]\n",
    "        m, self.ans = len(vals), 0\n",
    "        def dfs(idx, val):\n",
    "            if idx == m:\n",
    "                cnt = 2 * n // val\n",
    "                if cnt % 2 + val % 2 == 1 and cnt < val: self.ans += 1\n",
    "            else:\n",
    "                for _ in range(vals[idx][1] + 1):\n",
    "                    dfs(idx + 1, val)\n",
    "                    val *= vals[idx][0]\n",
    "        dfs(0, 1)\n",
    "        return self.ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        # res = 0\n",
    "        # if n>1:\n",
    "        #     for i in range(1,n//2+2):\n",
    "        #         if n%i == 0:\n",
    "        #             if i%2 == 1 and n/i>i//2:\n",
    "        #                 print(i)\n",
    "        #                 res += 1\n",
    "        #         elif (2*n)%i == 0 and i%2 == 0:\n",
    "        #             if (n)/i > i/2:\n",
    "        #                 print(i)\n",
    "        #                 res += 1\n",
    "        #     return res\n",
    "        # else:\n",
    "        #     return 1\n",
    "        res = 1\n",
    "        for k in range(2,n):\n",
    "            a = ((2*n)/k+1-k)/2\n",
    "            if a < 1:\n",
    "                break\n",
    "            elif a%1==0 and n==((2*a)+k-1)*k/2:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "def oddDivisorsNum(n: int) -> int:\n",
    "    \"\"\"n 拆分成若干连续整数的方法数/奇约数个数\"\"\"\n",
    "    res = 0\n",
    "    upper = int(sqrt(n)) + 1\n",
    "    for i in range(1, upper):\n",
    "        if n % i == 0:\n",
    "            if i & 1 == 1:\n",
    "                res += 1\n",
    "            if i * i < n and n // i & 1 == 1:\n",
    "                res += 1\n",
    "    return res\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        return oddDivisorsNum(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        def isKConsecutive(n: int, k: int) -> bool:\n",
    "            if k % 2:\n",
    "                return n % k == 0\n",
    "            return n % k and 2 * n % k == 0\n",
    "        res, k = 0, 1\n",
    "        while k * (k + 1) <= 2 * n:\n",
    "            if isKConsecutive(n, k):\n",
    "                res += 1\n",
    "            k += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        return 1 + sum(i%2==1 and n%i==0 and n//i-i//2>0 or i%2==0 and 2*n%i==0 and n%i!=0 and n/i-i//2>0 for i in range(2, int(2*n**.5)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        h=2\n",
    "        ans=0\n",
    "        while h*h<=2*n:\n",
    "            if (2*n+h-h*h)%(2*h)==0:\n",
    "                ans=ans+1\n",
    "            h=h+1\n",
    "        return ans+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        for k in range(1, int(sqrt(2 * n)) + 1):\n",
    "            if (2 * n) % k == 0 and (2 * n / k - (k - 1)) % 2==0:\n",
    "                ans +=1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        count = 0\n",
    "        k = 1\n",
    "        # k * (k + 1) // 2 is the first k value of sum \n",
    "        # For example first 3 value's sum is 3*4//2 = 6 = 1+2+3\n",
    "        # this number is smaller or equal to n\n",
    "        while k * (k + 1) // 2 <= n:\n",
    "            # If the rest of the number can be correctly divided by number of value k,\n",
    "            # then we have correct list\n",
    "            # for example, 15 - 6 = 9(remaining value)\n",
    "            # 9 % 3(which is k) = 0 !\n",
    "            # 9/3 = 3 which means that we can have 1+3,2+3,3+3 = 4,5,6\n",
    "            # 4+5+6 = 15 which satisfied our requirement\n",
    "            remain = n - (k * (k + 1) // 2)\n",
    "            if remain % k == 0:\n",
    "                count += 1\n",
    "            # continue to increase k\n",
    "            k += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        cnt = 1\n",
    "        for k in range(2, n):\n",
    "            a = (2 * n // k + 1 - k) // 2\n",
    "            if a < 1:\n",
    "                break\n",
    "            elif n == (2 * a + k - 1) * k // 2:\n",
    "                cnt += 1\n",
    "        return cnt\n",
    "        \n",
    "\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        return sum(t % i == 0 and (t // i + 1 - i) % 2 == 0 for i in range(1, int(sqrt(t)) + 1)) if (t := 2 * n) else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        def isKConsecutive(n: int, k: int) -> bool:\n",
    "            if k % 2:\n",
    "                return n % k == 0\n",
    "            return n % k and 2 * n % k == 0\n",
    "\n",
    "        ans = 0\n",
    "        k = 1\n",
    "        while k * (k + 1) <= n * 2:\n",
    "            if isKConsecutive(n, k):\n",
    "                ans += 1\n",
    "            k += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        import math\n",
    "        s=0\n",
    "        for i in range(2,math.ceil(math.sqrt(2*n))):\n",
    "            if (2*n)%i==0 and (2*n/i+1-i)%2==0:\n",
    "                s=s+1\n",
    "        return s+1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        while n & 1 == 0:\n",
    "            n >>= 1\n",
    "        ans = 1\n",
    "        d = 3\n",
    "        while d * d <= n:\n",
    "            e = 0\n",
    "            if n % d == 0:\n",
    "                while n % d == 0:\n",
    "                    n //= d\n",
    "                    e += 1\n",
    "                ans *= e + 1\n",
    "            d += 2\n",
    "        if n > 1:\n",
    "            ans *= 2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        cur = 0\n",
    "        ans = 0\n",
    "        if n == 1: return 1\n",
    "        for i in range(1, n):\n",
    "            cur += i\n",
    "            if cur > n: break\n",
    "            if (n - cur) % i == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        ss = ans = 0\n",
    "        for i in range(1, n + 1):\n",
    "            ss += i\n",
    "            if ss > n: break\n",
    "            if (n - ss) % i == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        # res = 0\n",
    "        # if n>1:\n",
    "        #     for i in range(1,n//2+2):\n",
    "        #         if n%i == 0:\n",
    "        #             if i%2 == 1 and n/i>i//2:\n",
    "        #                 print(i)\n",
    "        #                 res += 1\n",
    "        #         elif (2*n)%i == 0 and i%2 == 0:\n",
    "        #             if (n)/i > i/2:\n",
    "        #                 print(i)\n",
    "        #                 res += 1\n",
    "        #     return res\n",
    "        # else:\n",
    "        #     return 1\n",
    "        res = 1\n",
    "        for k in range(2,n):\n",
    "            a = ((2*n)/k+1-k)/2\n",
    "            if a < 1:\n",
    "                break\n",
    "            elif a%1==0 and n==((2*a)+k-1)*k//2:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\r\n",
    "        return sum(t % i == 0 and (t // i + 1 - i) % 2 == 0 for i in range(1, int(sqrt(t)) + 1)) if (t := 2 * n) else 0\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        count = 0\n",
    "        i, end = 1, n << 1\n",
    "        while i**2 < end:\n",
    "            if (n - (i + 1)*i/2)%i ==0:\n",
    "                count += 1\n",
    "            i += 1\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        def isKConsecutive(n: int, k: int) -> bool:\n",
    "            if k % 2:\n",
    "                return n % k == 0\n",
    "            return n % k and 2 * n % k == 0\n",
    "\n",
    "        ans = 0\n",
    "        k = 1\n",
    "        while k * (k + 1) <= n * 2:\n",
    "            if isKConsecutive(n, k):\n",
    "                ans += 1\n",
    "            k += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        cnt = 1\n",
    "        for k in range(2, n):\n",
    "            a = (2 * n // k + 1 - k) // 2\n",
    "            if a < 1:\n",
    "                break\n",
    "            elif n == (2 * a + k - 1) * k // 2:\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def consecutiveNumbersSum(self, n: int) -> int:\n",
    "        # while n & 1 == 0:\n",
    "        #     n >>= 1\n",
    "        # m = int(math.sqrt(n))\n",
    "        # ans = 1 if m * m == n else 2\n",
    "        # for i in range(3, m+1, 2):\n",
    "        #     if n % i == 0:\n",
    "        #         ans += 2\n",
    "        # return ans\n",
    "\n",
    "\n",
    "        ans = 0\n",
    "        for k in range(1, int(sqrt(2 * n)) + 1):\n",
    "            if (2 * n) % k == 0 and (2 * n / k - (k - 1)) % 2==0:\n",
    "                ans +=1\n",
    "        return ans\n",
    "\n",
    "\n",
    "        # res, i = 0, 1\n",
    "        # while n > 0:\n",
    "        #     res += n % i == 0\n",
    "        #     n -= i\n",
    "        #     i += 1\n",
    "        # return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
